package com.exam.dao.subject;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import com.exam.common.vo.ExamSubjectVO;
import com.exam.common.vo.page.PageModel;
import com.exam.common.vo.page.SystemContext;
import com.exam.dao.base.BaseDaoImpl;
import com.exam.entity.ExamSubject;

@Repository("examSubjectDaoImp")
public class ExamSubjectDaoImp extends BaseDaoImpl<ExamSubject> implements ExamSubjectDao{

	@SuppressWarnings("unchecked")
	@Override
	public PageModel<ExamSubjectVO> findByPage() {
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getPageSize();
		List<ExamSubjectVO> datas = new ArrayList<ExamSubjectVO>();
		//System.out.println(offset);
		PageModel<ExamSubjectVO> page = new PageModel<ExamSubjectVO>();
		List<ExamSubject> tempDatas = this.getSession().createQuery(" from ExamSubject ")
				.setFirstResult(offset).setMaxResults(pageSize)
				.list();
		for(ExamSubject subject:tempDatas){
			ExamSubjectVO subjectVO = new ExamSubjectVO();
			BeanUtils.copyProperties(subject, subjectVO);
			datas.add(subjectVO);
		}
		long totalSize = (long) this.getSession().createQuery(" select Count(*) from ExamSubject ").uniqueResult();
		page.setTotalSize(totalSize);
		page.setDatas(datas);
		page.setOffset(offset);
		page.setPageSize(pageSize);
		return page;
	}

	@Override
	public ExamSubjectVO findById(Integer id) {
		ExamSubject sub = (ExamSubject) this.getSession().createQuery("from ExamSubject where examSubjectId = :subId")
		.setParameter("subId", id).uniqueResult();
		ExamSubjectVO subVO = null;
		if(sub!=null){
			subVO = new ExamSubjectVO();
			BeanUtils.copyProperties(sub, subVO);
		}
		return subVO;
	}

}
